#define  _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

void SeqListInit(SeqList* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SeqListCheckCapacity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SeqListDateType* tmp = (SeqListDateType*)realloc(ps->arr, newcapacity*sizeof(SeqListDateType)*2);
		if (tmp == NULL)
		{
			perror("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->arr = tmp;
	}
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i =0; i<ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void SeqListPushBack(SeqList* ps, int x)
{
	assert(ps);
	SeqListCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
	//SeqListInsert(ps, ps->size,x);
}

void SeqListInsert(SeqList* ps, int pos, int x)
{
	assert(pos <= ps->size && pos>=0);
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->arr[end+1] = ps->arr[end];
		end--;
	}
	ps->arr[pos] = x;
	ps->size++;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	ps->size--;
	//SeqListErase(ps, ps->size-1);
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(pos >=0 && pos <= ps->size-1);
	int begin = pos+1;
	while (begin <= ps->size-1)
	{
		ps->arr[begin-1] = ps->arr[begin];
		begin++;
	}
	ps->size--;
}

void SeqListPushFront(SeqList* ps,int x)
{
	assert(ps);
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >=0)
	{
		ps->arr[end + 1] = ps->arr[end];
		end--;
	}
	ps->arr[0] = x;
	ps->size++;
	//SeqListInsert(ps,0,x);
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	int begin = 0;
	while (begin < ps->size - 1)
	{
		ps->arr[begin] = ps->arr[begin+1];
		++begin;
	}
	ps->size--;
	//SeqListErase(ps, 0);
}

void SeqListDestory(SeqList* ps)
{
	free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}